﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EGS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TeachersController : ControllerBase
    {
        private readonly EGSDbContext db;
        public TeachersController(EGSDbContext context)
        {
            db = context;
        }
        [HttpGet("GetAll")]
        public List<Teachers> GetAll()
        {
            return db.Teachers.ToList();
        }
        [HttpGet("{id}")]
        public Teachers? GetTeachersById(Guid id)
        {
            Teachers? teachers = new Teachers();
            teachers = db.Teachers.Where(t => t.ID == id).FirstOrDefault();
            return teachers;
        }
        [HttpGet("like/{name}")]
        public List<Teachers> GetTeachersLikeName(string name)
        {
            List<Teachers> teachers = new List<Teachers>();
            teachers = db.Teachers.Where(t => t.Name.Contains(name)).ToList();
            return teachers;
        }
        [HttpPost]
        public Result AddTeachers([FromBody] Teachers teacher)
        {
            Result result = new Result();
            db.Teachers.Add(teacher);
            result.Data = db.SaveChanges();
            return result;
        }
        public Result DelTeachers(Guid Id)
        {
            Result result = new Result();
            Teachers? teachers = new Teachers();
            teachers = db.Teachers.Where(r => r.ID == Id).FirstOrDefault();
            result.Data = db.SaveChanges();

            return result;
        }
        [HttpPut]
        public Result UpTeachers([FromBody] Teachers teacher)
        {
            Result result = new Result();
            db.Teachers.Update(teacher);
            result.Data = db.SaveChanges();

            return result;
        }
    }
    }
